'''
python3 main.sh 
'''
import sys
import pandas as pd
import csv
import cannot_lowering
import symbolize_log_check
import cannot_fuse
profiling_op_name_key = "Op Name"
profiling_task_time_key = "Task Duration(us)"
profiling_op_type_key = "OP Type"
output_column_names = ["op_name","op_type","module","reason_type","key_log","remark","task_time(us)"]
def check_outputs(outputs,module):
    for output in outputs:
        if len(output) != 6:
            print(f"[ERROR] check_outputs failed! module[{module}].")
            sys.exit(0)
def get_nofuse_nodes_time(profiling_file):
    df_profling = pd.read_csv(profiling_file)
    nodes_type = df_profling[[profiling_op_name_key,profiling_op_type_key]].set_index(profiling_op_name_key)[profiling_op_type_key].to_dict()
    df_profling = df_profling[~df_profling[profiling_op_type_key].isin(["AscBackend","FusedAscBackend"])]
    df_profling = df_profling[[profiling_op_name_key,profiling_task_time_key]]
    df_profling = df_profling.groupby(profiling_op_name_key)[profiling_task_time_key].mean().reset_index()
    return list(df_profling[profiling_op_name_key]), df_profling.set_index(profiling_op_name_key)[profiling_task_time_key].to_dict(),nodes_type
def main():
    outputs = [] #输出结果
    profiling_file = sys.argv[1]
    #nofuse_nodes是profling中未融合的算子
    #nodes_time记录这些算子的耗时
    nofuse_nodes,nodes_time,nodes_type = get_nofuse_nodes_time(profiling_file)

    log_file = sys.argv[2]
    with open(log_file, 'r', encoding='utf-8', errors='ignore') as file:
        inputs = file.readlines()
        no_symbolize_nodes,lifting_nodes,lifting_original_nodes = cannot_lowering.cannot_lowering(inputs,nofuse_nodes,outputs)
        if len(lifting_nodes) != len(lifting_original_nodes):
            print("[ERROR] len of lifting_nodes is not equal to len of lifting_original_nodes")
            sys.exit(0)
        if len(no_symbolize_nodes) + len(lifting_nodes) + len(outputs) != len(nofuse_nodes):
            print("[ERROR] len of nofuse_nodes is not equal to len of lowering outputs")
            sys.exit(0)
        check_outputs(outputs,"lowering")
        len_lowering_outputs = len(outputs)
        symbolize_log_check.cannot_symbolize(inputs,no_symbolize_nodes,outputs)
        if len_lowering_outputs + len(no_symbolize_nodes) != len(outputs):
            print("[ERROR] len of no_symbolize_nodes is not equal to len of symbolize outputs")
            sys.exit(0)
        check_outputs(outputs,"symbolize")
        cannot_fuse.cannot_fuse(inputs,lifting_nodes,lifting_original_nodes,outputs)
        if len(nofuse_nodes) != len(outputs):
            print("[ERROR] len of lifting_nodes is not equal to len of canfuse outputs")
            sys.exit(0)
        check_outputs(outputs,"canfuse")
    # write op_type task_time
    for output in outputs:
        output[1] = nodes_type[output[0]]
        task_time = nodes_time[output[0]]
        output.append(task_time)
        for item in output:
            if isinstance(item,str):
                item.replace("\n","")
    df_output = pd.DataFrame(outputs)
    df_output.columns = output_column_names
    df_output.to_csv("./reasons_for_nofuse.csv",index=False,encoding='utf-8-sig',quoting=csv.QUOTE_ALL)
if __name__ == "__main__":
    main()